﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessModel;
using EntityDataModel;
using System.ComponentModel;
using Controller.DataProvider;
using System.Data;
using System.Collections.ObjectModel;
using ManageStore.IViews;
using BaseUtilities;
using System.ComponentModel.DataAnnotations;

namespace Controller
{
    public class UnitCtr : BaseCtr, IDataErrorInfo
    {
        public UnitCtr(IUnitView pIUnitView)
        {
            this.IUnitView = pIUnitView;
        }

        #region Properties
        // IUnitView.
        public IUnitView IUnitView { get; set; }
        // UnitPro.
        private UnitPro _UnitPro;
        public UnitPro UnitPro
        {
            get
            {
                if (_UnitPro == null)
                {
                    _UnitPro = new UnitPro();
                }
                return _UnitPro;
            }
            set { _UnitPro = value;}
        }
        // Unit.
        private Unit _Unit;
        public Unit Unit
        {
            get
            {
                if (_Unit == null)
                {
                    _Unit = new Unit();
                }
                return _Unit;
            }
            set 
            {
                if (_Unit != value)
                {
                    _Unit = value;
                    OnPropertyChanged("Unit");
                }
            }
        }
        // LstErrorFields
        private Dictionary<string, Dictionary<string, string>> _DicError;
        public Dictionary<string, Dictionary<string, string>> DicError
        {
            get
            {
                if (_DicError == null)
                    _DicError = new Dictionary<string, Dictionary<string, string>>();
                return _DicError;
            }
            set
            {
                _DicError = value;
            }
        }
        
        #endregion Properties

        #region Binding properties
        // Id
        public int Id
        {
            get
            {
                return Unit.Id;
            }
            set
            {
                if (Unit.Id != value)
                {
                    Unit.Id = value;
                    OnPropertyChanged("Id");
                }
            }
        }
        // UnitName
        //[Required(ErrorMessage = "Title is required.")]
        //[StringLength(2, ErrorMessage="sdfjksd")]
        public string UnitName
        {
            get
            {
                return Unit.Name;
            }
            set
            {
                if (Unit.Name != value)
                {
                    Unit.Name = value;
                    OnPropertyChanged("UnitName");
                }
            }
        }
        // LstUnit.
        private ObservableCollection<Unit> _LstUnit;
        public ObservableCollection<Unit> LstUnit
        {
            get
            {
                if (_LstUnit == null)
                {
                    _LstUnit = new ObservableCollection<BusinessModel.Unit>(GetAll().ToResult<IList<Unit>>());
                }
                return _LstUnit;
            }
            set
            {
                if (_LstUnit != value)
                {
                    _LstUnit = value;
                    OnPropertyChanged("LstUnit");
                }
            }
        }
       
        #endregion Binding properties

        private void LoadListUnit()
        {
            LstUnit = new ObservableCollection<BusinessModel.Unit>(GetAll().ToResult<IList<Unit>>());
        }

        #region View Handlers
        //----------------------------------------
        public bool MtdNew(params object[] pParams)
        {
            bool result = false;
            if (IUnitView.BeforeHandler("EvtNew"))
            {
                LoadListUnit();
                IUnitView.ReloadData();
                Unit = new Unit();
                DicError.Clear();
                IUnitView.CompletedHandler("EvtNew");
            }
            return result;
        }
        //----------------------------------------
        public bool MtdEdit(params object[] pParams)
        {
            bool result = false;
            if (IUnitView.BeforeHandler("EvtEdit"))
            {
                if (pParams.Count() == 1)
                {
                    Unit = pParams[0] as Unit;
                    if (Unit == null)
                    {
                        IUnitView.ErrorHandler("EvtEdit");
                    }
                    else
                    {
                        DicError.Clear();
                        IUnitView.CompletedHandler("EvtEdit");
                    }
                }
            }
            return result;
        }
        //----------------------------------------
        public bool MtdDelete(params object[] pParams)
        {
            bool result = false;
            if (IUnitView.BeforeHandler("EvtDelete"))
            {
                EntityKey oEntityKey = new EntityKey(BaseProvider.QualifyName + "." + UnitPro.EntityName, "Id", Unit.Id);
                ResultModel oResultModel = UnitPro.DeleteById(oEntityKey);
                if (oResultModel.HasError)
                {
                    IUnitView.ErrorHandler("EvtDelete", oResultModel);
                }
                else
                {
                    LstUnit = new ObservableCollection<BusinessModel.Unit>(GetAll().ToResult<IList<Unit>>());
                    IUnitView.CompletedHandler("EvtDelete");
                    result = true;
                }
            }
            return result;
        }
        //----------------------------------------
        public bool MtdSave(params object[] pParams)
        {
            // Check validate.
            if (DicError.Count > 0)
            {
                IUnitView.ErrorHandler("EvtSave", DicError);
                return false;
            }
            bool result = false;
            if (IUnitView.BeforeHandler("EvtSave"))
            {
                if (Unit.Id == default(int))
                {
                    Unit.CreatedBy = ControllerConfig.Default.UserName;
                    Unit.CreatedDate = DateTime.Now;
                }
                Unit.ModifiedBy = ControllerConfig.Default.UserName;
                Unit.ModifiedDate = DateTime.Now;

                EntityKey oEntityKey = new EntityKey(BaseProvider.QualifyName + "." + UnitPro.EntityName, "Id", Unit.Id);
                ResultModel oResultModel = UnitPro.Save(Unit, oEntityKey);
                if (oResultModel.HasError)
                {
                    IUnitView.ErrorHandler("EvtSave", oResultModel);
                }
                else
                {
                    LstUnit = new ObservableCollection<BusinessModel.Unit>(GetAll().ToResult<IList<Unit>>());
                    IUnitView.CompletedHandler("EvtSave", oResultModel.Result);
                    result = true;
                }
            }
            return result;
        }
        //----------------------------------------
        public ResultModel GetById(int pUnitId)
        {
            EntityKey oEntityKey = new EntityKey(BaseProvider.QualifyName + "." + UnitPro.EntityName, "Id", pUnitId);
            return UnitPro.GetById(oEntityKey);
        }
        //----------------------------------------
        public ResultModel GetAll()
        {
            return UnitPro.GetAll();
        }
        #endregion View Handlers

        #region IDataErrorInfo Members
        private string _Error;
        public string Error
        {
            get 
            {
                return _Error;
            }
        }
        public string this[string columnName]
        {
            get
            {
                string error = string.Empty;
                Dictionary<string, string> dicEle = null;
                switch (columnName)
                {
                    case "UnitName":
                        if (string.IsNullOrWhiteSpace(UnitName))
                        {
                            error = "Tên đơn vị tính không được rỗng.";
                            if (DicError.Keys.Contains(columnName))
                            {
                                dicEle = DicError[columnName];
                            }
                            else
                            {
                                dicEle = new Dictionary<string, string>();
                                DicError.Add(columnName, dicEle);
                            }
                            if (!dicEle.Keys.Contains(EnumValidate.Empty.ToString()))
                                dicEle.Add(EnumValidate.Empty.ToString(), error);
                        }
                        else
                        {
                            if (DicError.Keys.Contains(columnName))
                                DicError.Remove(columnName);
                        }
                        break;
                }
                _Error = error;
                IUnitView.ValidationHandler(columnName, dicEle);
                return error;
            }
        }

        #endregion
    }
}
